package com.yonyou.crm.sact.taskcard.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sact.taskcard.entity.SactTaskcardVO;
import com.yonyou.crm.sact.taskcard.rmitf.ISactTaskcardRmService;
import com.yonyou.crm.sys.modules.rmitf.IBiztypeRmService;

@RestController
@RequestMapping(value = "/sact/taskcards")
public class TaskcardCtrl {
	
	@Autowired
	private ISactTaskcardRmService service;
	@Autowired
	private IBiztypeRmService biztypeService;
	
	/**
	 * 根据ID数组返业务对象和业务类型
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "modules", method = RequestMethod.GET)
	public Object getDocAndDetailList(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = paramObj.getString("ids");
		if (idArray == null) {
			throw new CrmMessageException("ids参数为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("1","Contact");//联系人
		map.put("2","Clue");//线索
		map.put("3","Opportunity");//商机
		map.put("4","CompetitionCollection");//竞品采集
//		Map<String, Object> modulesMap = biztypeService.getDocAndDetailList(map);
		Map<String, Object> modulesMap = new HashMap<String, Object>();
		modulesMap.put("1","联系人");
		modulesMap.put("2","线索");
		modulesMap.put("3","商机");
		modulesMap.put("4","竞品采集");
		JSONObject result = new JSONObject();
		result.put("enumData", modulesMap);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 任务卡列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object queryPage(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		@SuppressWarnings("unchecked")
		Map<String, Object> condMap = (Map<String, Object>) paramObj.get("searchMap");
		//本页面不进行分页查询，page参数在mapper中不使用
		List<SactTaskcardVO> taskcardList = service.getSactTaskcardList(condMap);
		JSONObject result = new JSONObject();
		result.put("data", taskcardList);
		return ResponseUtil.getObject(result);
		
	}
	
	/**
	 * 新增任务卡
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		SactTaskcardVO taskcard = RequestUtil.getVO(request, SactTaskcardVO.class);
		String validMsg = validator(taskcard);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		taskcard = service.insertTaskcard(taskcard);
		if (taskcard.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}
		return ResponseUtil.getObject(taskcard);
	}
	
	/**
	 * 校验
	 * @param vo
	 * @return
	 */
	private String validator(SactTaskcardVO vo) {
		if (vo == null) {
			return "转换实体失败";
		}
		if(vo.getMtObjId().equals(null) || vo.getMtObjId().equals(new Long(0))){
			return "业务对象不可为空";
		}
		if(vo.getMtBiztypeId().equals(null) || vo.getMtBiztypeId().equals(new Long(0))){
			return "业务类型不可为空";
		}
		return "";
	}
	
	/**
	 * 批量删除任务卡
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object delete(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = paramObj.getString("ids");		

		if (idArray == null) {
			throw new CrmMessageException("ids参数为空");
		}
		String[] idStrs = idArray.split(",");
		@SuppressWarnings("unchecked")
		Map<String, Object> searchMap = (Map<String, Object>)paramObj.get("searchMap");
		List<SactTaskcardVO> taskcardList = service.deleteSactTaskcardByIDs(idStrs,searchMap);
		JSONObject result = new JSONObject();
		result.put("data", taskcardList);
		return ResponseUtil.getObject(result, PCCodeConstant.Success,"数据删除成功");
	}
	
	/**
	 * 修改任务卡
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		SactTaskcardVO taskcard = RequestUtil.getVO(request, SactTaskcardVO.class);	
		if (taskcard == null) {
			throw new CrmMessageException("id不能为空");
		}
		taskcard.setId(id);
		taskcard = service.updateSactTaskcard(taskcard);
		return ResponseUtil.getObject(taskcard);
	}
	
	/**
	 * 批量停用/启用任务卡
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idStr = paramObj.getString("id");
		if (idStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		String[] idArray = idStr.split(",");
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		Integer enableState = paramObj.getInt("enableState");
		if (enableState == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}
		Map<String, Object> paramMap = (Map<String, Object>) paramObj.get("searchMap");
		List<SactTaskcardVO> taskcardList = service.batchUpdateEnableState(idArray, enableState,paramMap);
		JSONObject result = new JSONObject();
		result.put("data", taskcardList);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 根据业务对象ID寻找业务类型集合
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "biztypes", method = RequestMethod.GET)
	public Object getBiztypeListByModule(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idStr = paramObj.getString("moduleId");
		if (idStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "moduleId参数为空");
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result = service.getBiztypeListByModule(Long.parseLong(idStr));
		return ResponseUtil.getObject(result);
	}
}
